<?php

namespace Admin\Controller;


use Admin\Common\Category;
use Admin\Logic\CateLogic;
use Admin\Logic\FoodCateLogic;
use Admin\Logic\FoodLogic;
use Admin\Logic\StockLogic;

class FoodController extends CommonController
{
    // 展示列表
    public function index()
    {
        if (!empty(I('get.goods_name_query'))) {
            // 根据商品名称查询
            $food = FoodLogic::getStockByName(I('get.goods_name_query'));
            $goods_name_query = I('get.goods_name_query');
        } else {
            // 获取库存列表
            $food = FoodLogic::getAll();

            $goods_name_query = '';
        }


        $this->assign(['food'=>$food,'goods_name_query'=>$goods_name_query]);
        $this->display();
    }


    // 展示修改页面
    public function edit()
    {
        $id = I('get.id', 0, 'intval');
        // 获取库存信息
        $food = FoodLogic::getStockById($id);
        $stock = StockLogic::getAll();

        // 价格除以100
        $food['price'] = $food['price']/100;

        // 大中小份损耗
        $food['big'] = json_decode($food['loss'], true)['大'];
        $food['medium'] = json_decode($food['loss'], true)['中'];
        $food['small'] = json_decode($food['loss'], true)['小'];


        // 获取等级分类
        $food_cate = Category::toLevel(FoodCateLogic::getShowCate());

        // 获取库存的等级分类
        $cate = Category::toLevel(CateLogic::getShowCate());

        // 获取菜谱明细
        $detail = FoodLogic::getFoodDetail($id);


        $this->assign(['food'=>$food,'food_cate'=>$food_cate,'cate'=>$cate,'detail'=>$detail,'stock'=>$stock]);
        $this->display();
    }

    // 展示添加页面
    public function add()
    {
        // 所有分类
        $stock = StockLogic::getAll();
        $cate = Category::toLevel(FoodCateLogic::getShowCate());
        $this->assign(['cate'=>$cate, 'stock'=>$stock]);
        $this->display();
    }


    /**
     * 执行修改库存的操作
     * @return string
     */
    public function update()
    {
        $data = I('post.');

        $data['food'] =  json_decode(trim(htmlspecialchars_decode($data['food']),"'"), true);
        $data['loss'] = json_encode(['大'=>$data['bigLoss'],'中'=>$data['mediumLoss'],'小'=>$data['smallLoss']]) ;

        // 修改数据
        $result = FoodLogic::update($data);

        if($result){
            return $this->ajaxSuccess();
        }else{
            return $this->ajaxError();
        }

    }




    /**
     * 执行添加库存的操作
     * @return string
     */
    public function create()
    {
        $data = I('post.');
        $data['food'] =  json_decode(trim(htmlspecialchars_decode($data['food']), "'"), true);
        $data['loss'] = json_encode(['big' => $data['bigLoss'], 'medium' => $data['mediumLoss'], 'small' => $data['smallLoss']]) ;
        // 添加数据
        $result = FoodLogic::create($data);
        if ($result) {
            return $this->ajaxSuccess();
        } else {
            return $this->ajaxError();
        }
    }


    public function delete()
    {
        $id = I('get.id');

        $data = FoodLogic::delete($id);

        if ($data > 0) {
            return $this->ajaxSuccess();
        } else {
            return $this->ajaxError('删除失败，请重试');
        }
    }


    /**
     * 根据选择分类显示该分类下所有材料
     * @param int $id  材料分类ID
     * @return mixed
     */
    public function ajaxStock()
    {
        $cate_id = I('get.id', 0, 'intval');

        //根据当前分类求出该分类下的所有子分类
        $cate = CateLogic::getAll(true);
        $ids = Category::getChildsId($cate, $cate_id);

        if (!empty($ids)) { //如果查到数据就使用,拼接
            $ids = implode(',', $ids);
        } else {
            $ids = implode(',', [$cate_id]);
        }

        //取出符合条件的材料
        $stock = StockLogic::ajaxFood($ids);

        if ($stock == false) {
            return $this->ajaxError('未查询到相关数据');
        }

        return $this->ajaxSuccess('success', $stock);
    }

    /**
     * 展示材料下拉列表
     */
    public function test()
    {
        $cate = CateLogic::getAll();

        $this->assign(['cate' => $cate]);
        $this->display();
    }

    /**
     * 异步根据库存分类获取所属库存列表
     * @return [type] [description]
     */
    public function ajaxStockList()
    {
        $cate_id = I('get.cate_id');
        $stock = StockLogic::getStockByName(null, $cate_id);
        $this->assign('stock', $stock);
        $this->display();
    }

    public function getUnit()
    {
        // 接受库存id
        $id = I('post.id');

        // 查询 计量单位
        $unit = D('Stock')
            ->where(['id'=>$id])
            ->field('unit')
            ->find();

        $this->ajaxSuccess($unit);
    }

}